#!/bin/sh
# Slackware Build Script for xmltv_prereq

# Copyright 2006-2009  David Somero (dsomero@hotmail.com) Athens,TN USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=xmltv_prereq
VERSION=8
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=${TMP}/package-${PRGNAM}
OUTPUT=${OUTPUT:-/tmp}

## List of modules to compile
MODULES="
00_Class-MethodMaker-2.09
00_HTML-Tagset-3.10
00_TermReadKey-2.30
00_Tk-804.027
00_XML-LibXML-Common-0.13
00_XML-NamespaceSupport-1.09
01_HTML-Parser-3.56
01_XML-SAX-0.15
01_libwww-perl-5.805
DateManip-5.44
HTML-TableExtract-2.10
HTML-Tree-3.23
Lingua-EN-Numbers-Ordinate-1.02
Lingua-Preferred-0.2.4
PerlIO-gzip-0.18
SOAP-Lite-0.69
Term-ProgressBar-2.09
Tk-TableMatrix-1.23
Unicode-String-2.09
XML-LibXML-1.62
XML-Twig-3.29
XML-Writer-0.602
WWW-Mechanize-1.20
"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2 
cd $PRGNAM-$VERSION
chown -R root:root . 
chmod -R u+w,go+r-w,a-s .

eval $(perl '-V:vendorlib')
export PERL5LIB+=$PKG/$vendorlib

for MODULE in $MODULES ; do
( cd $MODULE
  if  [ "$MODULE" = "00_Class-MethodMaker-2.09" ]; then
    perl Makefile.PL 
  elif  [ "$MODULE" = "WWW-Mechanize-1.20" ]; then
    perl Makefile.PL --mech-dump 
  else
    perl Makefile.PL -n 
  fi 
  make 
  make pure_vendor_install DESTDIR=$PKG/
  mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
  cp -a README $PKG/usr/doc/$PRGNAM-$VERSION/README.$MODULE || true
) || exit 1 ; done

mv $PKG/usr/share/man $PKG/usr
rmdir $PKG/usr/share

( cd $PKG/usr/man
  find . -name "*.?" -type f 2> /dev/null | xargs gzip -9 2> /dev/null
  for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done
)

# Remove perlocal.pod and .packlist from $PKG
( cd $PKG
  find . -name "perllocal.pod" -o -name ".packlist" -o -name "*.bs" | xargs rm -f
)

cat README > $PKG/usr/doc/$PRGNAM-$VERSION/README
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chmod 644 $PKG/usr/doc/$PRGNAM-$VERSION/*

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
